/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.socio.behaviors;

import java.util.Arrays;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;
import com.socio.model.Day;
import com.socio.utils.FieldLoader.FieldLoaderFields;

/**
 *
 * @author potlow
 */
public class DayKeyBehavior extends DayBehavior {

    @Override
    public Object read(Object value, List<FieldLoaderFields> fields) {
    	PersistenceManager pm = pmf.getPersistenceManager();
    	
        Key key;
        if (value instanceof Key) {
            key = (Key) value;
        } else {
            throw new IllegalArgumentException("Search value must be a Key");
        }

        // check cache first if not there get from db and put into cache
        Object day = null;
        if(cache.containsKey(key)) {
        	day = cache.get(key);
        } else {
        	try{
        		day = pm.getObjectById(Day.class, key);
        		if(day != null) {
        			cache.put(key, day);
        		}
        	} finally {
        		pm.close();
	        }
        }
        
        return Arrays.asList(day);
    }
}
